<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class PayController extends Controller
{
    public function add(){
    	$title = '支付管理';
    	return view('admin.pay.add',['title'=>$title]);
    }

    public function insert(Request $request){
    	
    	$data = $request -> except(['_token']);
    	//判断用户名是否为空
    	//laravel内置验证
    	$this ->validate($request,[
    		'username'=>'required',
    		'id'=>'required',
    		'paytime'=>'required',
    		'payway'=>'required',
    		'logo'=>'required'
    		],[
    		'username.required'=>'用户昵称不能为空',
    		'id.required'=>'用户id不能为空',
    		'paytime.required'=>'支付时间不能为空',
    		
    		'payway.required'=>'支付方式不能为空',
    		
    		'logo.required'=>'支付方式logo不能为空']);
    	//处理pic字段
    	if($request ->hasfile('logo'))
    	{
    		if($request ->file('logo')->isValid())
				{
					$suffix = $request->file('logo')->getClientOriginalExtension();
						//生成随机文件名称
						$filename = time().mt_rand(10000,99999).'.'.$suffix;
						//yidoing文件
						$request -> file('logo')->move('./uploads/pay',$filename);
						$data['logo'];
						//拼接pic路径
						$data['logo'] = '/uploads/pay/'.$filename;
				}
			}
		
			//执行添加操作
			$res = DB::table('pay')->insert($data);
			//
			if($res)
			{
				return redirect('/admin/pay');
			}else{
				return back();
			}  	

    }

    //index
    public function index(Request $request)
    {
        
    	//查询数据
    	$data =  DB::table('pay')
         ->where('username','like','%'.$request->input('keywords').'%')
         ->paginate($request->input('num',5));
         // ->paginate(5);

    	$title = '支付列表';
    	return view('admin.pay.index',['title'=>$title,'data'=>$data,'request'=>$request->all()]);
    }

    public function edit($id)
    {

    	//查询数据
    	$data =DB::table('pay') -> where('id',$id)->first(); 
    	$title = '用户编辑';
    	return view('admin.pay.edit',['title'=>$title,'data'=>$data]);
    }
    

    	 public function update(Request $request){
    	 	$data = $request -> except(['_token','id','oldlogo']);
    	 	//laravel内置验证
    	 	$id = $request ->input('id');

    	$this ->validate($request,[
    		
    		'logo'=>'required'
    		],[
    		
    		'logo.required'=>'logo不能为空']);
    	//处理pic字段
    	if($request ->hasfile('logo'))
    	{
    		if($request ->file('logo')->isValid())
				{
					$suffix = $request->file('logo')->getClientOriginalExtension();//获取图片格式
						//生成随机文件名称
						$filename = time().mt_rand(10000,99999).'.'.$suffix;
						//yidoing文件
						$request -> file('logo')->move('./uploads/pay',$filename);
						$data['logo'];
						//拼接pic路径
						$data['logo'] = '/uploads/pay/'.$filename;
						//删除老图
						//
						// dd($data);
						$oldlogo = '.'.$request -> input('oldlogo');
						// dd($request ->all());
						if(file_exists($oldlogo))
						{
							unlink($oldlogo);
						}
				}
			}
			//执行添加操作
			$res = DB::table('pay')->where('id',$id)->update($data);
			//
			if($res)
			{
				return redirect('/admin/pay')->with(['info'=>'更新成功']);
			}else{
				return back();
			}  
    	 }
    	 public function delete($id){
    	 	$res= DB::table('pay') ->where('id',$id)->delete();
    	 	if($res)
			{
				return redirect('/admin/pay')->with(['info'=>'删除成功']);
			}else{
				return redirect('/admin/pay')->with(['info'=>'删除不成功']);
			} 
    	 }
	}
